package com.example.zbh.dsliao.ui.adapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.NetSongList;
import com.example.zbh.dsliao.ui.activty.DownLoaderActivity;

import java.util.List;

/**
 * Created by yaolin on 15/11/30.
 */
public class DownLoadAdapter extends RecyclerView.Adapter<DownLoadAdapter.DownLoadViewHolder> {
    public static final String ACTION_DOWNLOAD_MAX_PROGRESS = "my_download_max_progress";
    public static final String ACTION_DOWNLOAD_CURRENT_PROGRESS = "my_download_current_progress";
    public static final String ACTION_DOWNLOAD_SUCCESS = "my_download_success";
    public static final String ACTION_DOWNLOAD_FAIL = "my_download_fail";
    private int current = 0;
    private int max = 0;
    private List<NetSongList> list;
    private DownLoadBroadcastReceiver receiver;
    private Context context;
    private int progress = 0;




    public DownLoadAdapter(Context context) {
        this.context = context;
        receiver = new DownLoadBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_DOWNLOAD_CURRENT_PROGRESS);
        filter.addAction(ACTION_DOWNLOAD_MAX_PROGRESS);
        filter.addAction(ACTION_DOWNLOAD_FAIL);
        filter.addAction(ACTION_DOWNLOAD_SUCCESS);


        context.registerReceiver(receiver, filter);
    }

    public void addData(List<NetSongList> list){
        this.list = list;
        notifyDataSetChanged();
    }
    public boolean isLength(){
        return list != null && list.size() > 0;
    }

    @Override
    public DownLoadViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyclerview_down_load_item, null);
        return new DownLoadViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DownLoadViewHolder holder, int position) {
        if (isLength()){
            NetSongList songList = list.get(position);
            holder.song_name_tv.setText(songList.getName());
            holder.progressBar.setMax(max);

            holder.progressBar.setProgress(current);

        }

    }




    public class DownLoadBroadcastReceiver extends BroadcastReceiver  {


        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if (action.equals(ACTION_DOWNLOAD_CURRENT_PROGRESS)) {//当前进度
                current = intent.getExtras().getInt("current");
                Log.i("ssssssssss", current + "");
                DownLoadAdapter.this.notifyDataSetChanged();


            } else if (ACTION_DOWNLOAD_MAX_PROGRESS.equals(action)) {//最大进度
                max = intent.getExtras().getInt("maxIndex");
                Log.i("mmmmmmmmmmm", max + "");
                DownLoadAdapter.this.notifyDataSetChanged();

            } else if (action.equals(ACTION_DOWNLOAD_FAIL)) {//下载失败

            } else if (action.equals(ACTION_DOWNLOAD_SUCCESS)) {//下载成功

            }


        }

    }

    @Override
    public int getItemCount() {
        return isLength() ? list.size() : 0;
    }


    class DownLoadViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView song_name_tv;
        private ProgressBar progressBar;
        private ImageView img_icon;

        public DownLoadViewHolder(View itemView) {
            super(itemView);
            song_name_tv = (TextView) itemView.findViewById(R.id.song_name_tv);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);

            img_icon = (ImageView) itemView.findViewById(R.id.img_icon);
            img_icon.setOnClickListener(this);

            if (onShowProgressBarIndex != null){
                onShowProgressBarIndex.onShowProgressBar(progressBar);
            }

        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                int position = getLayoutPosition();  //更新位置

                NetSongList n = list.get(position);
                switch (v.getId()) {
                    case R.id.img_icon:
                        list.remove(position);   //从集合里移除
                        notifyItemRemoved(position);  //刷新
                        if( listener != null){
                            listener.OnRemoveClick(list);
                        }
                break;
                }
            }
        }
    }
    public interface OnShowProgressBarIndex{
        void onShowProgressBar(ProgressBar progressBar);
    }
    private OnShowProgressBarIndex onShowProgressBarIndex;
    public void OnShowProgressBar(OnShowProgressBarIndex onShowProgressBarIndex){
        this.onShowProgressBarIndex = onShowProgressBarIndex;

    }

    //监听
    public interface OnRemovClickListener {
        void OnRemoveClick(List<NetSongList> list);
    }

    private OnRemovClickListener listener;

    /**
     * 监听事件
     **/
    public void setOnRemovClickListener(OnRemovClickListener listener) {
        this.listener = listener;
    }

}
